<?php

namespace frontend\modules\user\controllers;

use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\web\Controller;

/**
 * Default controller for the `user` module
 */
class DefaultController extends Controller
{
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'update-sign-key' => ['post'],
                ],
            ],
        ]);
    }

    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index', [
        	'user' => \Yii::$app->user->identity,
        ]);
    }

    /**
     * 更新签名钥匙
     * @return \yii\web\Response
     */
    public function actionUpdateSignKey()
    {
        $user = \Yii::$app->user->identity;
        $user->sign_key = \Yii::$app->security->generateRandomString();
        if ($user->save()){
            \Yii::$app->session->addFlash("success", "更换签名钥匙成功");
        }else{
            \Yii::$app->session->addFlash("error", "更换签名钥匙失败");
        }
        return $this->goHome();
    }
}
